//
//  CityViewController.m
//  Sky
//
//  Created by chen on 16/6/28.
//  Copyright (c) 2016年 chen. All rights reserved.
//

#import "CityViewController.h"

@interface CityViewController ()<UITableViewDataSource,UITableViewDelegate, UIAlertViewDelegate>

@property (strong,nonatomic) NSMutableArray *cityNames;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic) NSInteger selectedRow;
@end

static NSString *CityCellIdentifier=@"CityCellIdentifier";

@implementation CityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.cityNames = [[NSMutableArray alloc]init];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CityCellIdentifier];
}

-(void)viewDidAppear:(BOOL)animated{
    NSString *filePath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"SelectedCities.plist"];
    self.cityNames=[NSMutableArray arrayWithContentsOfFile:filePath];
    [self.tableView reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.cityNames count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CityCellIdentifier forIndexPath:indexPath];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CityCellIdentifier];
    }
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    cell.textLabel.text=self.cityNames[indexPath.row];
    return cell;
}

-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.selectedRow = indexPath.row;
    
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        action.backgroundColor = [UIColor redColor];
        UIAlertView *alert = [[UIAlertView alloc]init];
        alert.delegate = self;
        alert.title = @"提示";
        alert.message = @"确定删除";
        [alert addButtonWithTitle:@"取消"];
        [alert addButtonWithTitle:@"确定"];
        alert.cancelButtonIndex = 0;
        [alert show];
    }];
    return @[deleteAction];
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [self.cityNames removeObjectAtIndex:self.selectedRow];
        NSString *filePath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"SelectedCities.plist"];
        [self.cityNames writeToFile:filePath atomically:YES];
        
        [self.tableView reloadData];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"CityListDidChanged" object:nil];
    }
}

@end
